package mango.orm;

import mango.functions.FuncString;

public class SQLParameter implements Cloneable {
    private String paramName;
    private int index = 0;
    private Object val;
    private int jdbcType;
    
    private SQLParameter() {
    }

    public SQLParameter(int index, Object val) {
        if (index <= 0) {
            throw new OrmException("Parameter Index Must Be > 0");
        }
        this.index = index;
        this.val = FuncString.encodeIfString(val);
        this.jdbcType = java.sql.Types.INTEGER;
    }

    public SQLParameter(int index, Object val, int type) {
        if (index <= 0) {
            throw new OrmException("Parameter Index Must Be > 0");
        }
        this.index = index;
        this.val = val;
        this.jdbcType = type;
    }

    public SQLParameter(String paramName, Object val) {
        if (FuncString.isBlank(paramName)) {
            throw new OrmException("Parameter Name Can't Be Blank");
        }
        this.paramName = paramName;
        this.val = FuncString.encodeIfString(val);
        this.jdbcType = java.sql.Types.INTEGER;
    }

    public SQLParameter(String paramName, Object val, int type) {
        if (FuncString.isBlank(paramName)) {
            throw new OrmException("Parameter Name Can't Be Blank");
        }
        this.paramName = paramName;
        this.val = val;
        this.jdbcType = type;
    }

    public int getIndex() {
        return this.index;
    }
    
    public SQLParameter setIndex(int index) {
        this.index = index;
        return this;
    }

    public Object getVal() {
        return this.val;
    }

    public int getJdbcType() {
        return this.jdbcType;
    }

    public String getParamName() {
        return this.paramName;
    }
    
    public SQLParameter clone(){
        SQLParameter p = new SQLParameter();
        p.index = this.index;
        p.jdbcType = this.jdbcType;
        p.paramName = this.paramName;
        p.val = this.val;
        return p;
    }

    @Override
    public String toString() {
        if (FuncString.isBlank(this.paramName)) {
            return "{index:" + this.index + ",value:" + this.val + "}";
        }
        else {
            return "{index:" + this.index + ",name:" + this.paramName + ",value:" + this.val + "}";
        }
    }
}
